//引入模板
const express=require('express');
//引入连接池模板
const pool=require('../pool.js');
//创建路由器对象
const u=express.Router();
//1.用户留言联系
u.post("/userMessage",(req,res)=>{
	var _varieties=req.body.varieties;
	var _uname=req.body.uname;
	var _phone=req.body.phone;
	var _email=req.body.email;
	var _message=req.body.message;
	var sql="insert into pet_user values(null,?,?,?,?,?)";
	pool.query(sql,[_varieties,_uname,_phone,_email,_message],(err,result)=>{
		if (err) throw err;
		res.send({ message: 'ok', code: 200 ,result});
		});
	});
//2、用户登录
u.post("/login", (req, res) => {
	console.log(req.body);
	var _uname = req.body.uname;
	var _upwd = req.body.upwd;
	console.log(_uname,_upwd);
	var sql = "select * from pet_users where uname=? and upwd=?";
	pool.query(sql, [_uname, _upwd], (err, result) => {
		if (err) throw err;
		if (result.length > 0) {
			console.log("1"+result.length);
			res.send({message:'ok',code:200,result:result[0]});
		} else {
			console.log("2"+result.length);
			res.send({message:'login failed',code:201});
		}
	});
});
//3、用户注册
u.post("/reg",(req,res)=>{
	var _uname=req.body.uname;
	var _upwd=req.body.upwd;
	var _rname=req.body.rname;
	var _age=req.body.age;
	var _phone=req.body.phone;
	var _email=req.body.email;
	var  sql = "select * from pet_users where uname = ?";
	pool.query(sql, [_uname], (error, result) => {
	  if (error) throw error;
		if (result.length==0){
	var sql="insert into pet_users values(null,?,?,?,?,?,?)";
	pool.query(sql,[_uname,_upwd,_rname,_age,_phone,_email],(err,result)=>{
		if (err) throw err;
		res.send({ message: 'ok', code: 200 });
		});
	}else{
		res.send({ message: 'user exists', code: 201 });
	};
});
});
//导出路由器对象
module.exports=u;